import 'package:flutter/material.dart';
import 'package:interview/components/search_box.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/views/home/components/home_banner.dart';
import 'package:interview/views/home/components/clock_in.dart';
import 'package:interview/views/home/components/home_calendar.dart';
import 'package:interview/views/home/components/home_category.dart';
import 'package:interview/views/home/components/home_scan.dart';

class Home extends StatefulWidget {
  const Home({super.key});
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home>  {
  // 开启banner 静态图片的预加载
  bool _imagesPrecached = false;
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 确保只预加载一次
    if (!_imagesPrecached) {
      _precacheImages();
      _imagesPrecached = true;
    }
  }

  Future<void> _precacheImages() async {
    // 使用 context 安全预加载图片
    await Future.wait([
      precacheImage(const AssetImage('${AppEnumAssets.basePath}banner_ai.png'), context),
      precacheImage(const AssetImage('${AppEnumAssets.basePath}banner_pj.png'), context),
      precacheImage(const AssetImage('${AppEnumAssets.basePath}banner_qa.png'), context),
    ]);
  }

  Future<void> _loadWidgetAsync() async {
    // 仅延迟渲染500ms
    await Future.delayed(const Duration(milliseconds: 100));
  }


  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
    return StatusBarWrapper(
      statusBarColor: theme.subjectMix1!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark ? Brightness.light : Brightness.dark,
      child: SafeArea(
        child: Column(
          children: [
            Container(
              color: theme.subjectMix1,
              padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
              child: Column(
                children: [
                  FutureBuilder(
                    future: _loadWidgetAsync(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return const SizedBox.shrink();
                      }
                      return Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          HomeScan(theme: theme),
                          const Expanded(
                            child: SearchBox(),
                          ),
                          ClockIn()
                        ],
                      );
                    },
                  ),
                  FutureBuilder(
                    future: _loadWidgetAsync(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return const SizedBox.shrink();
                      }
                      return const HomeBanner();
                    },
                  ),
                  FutureBuilder(
                    future: _loadWidgetAsync(),
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                        return const SizedBox.shrink();
                      }
                      return const HomeCalendar();
                    },
                  ),
                ],
              ),
            ),
            Expanded(
              child: Container(
                padding: const EdgeInsets.only(bottom:8),
                color: theme.subjectPure,
                child: const HomeCategory()
              )
            )
          ],
        )
      )
    );
  }
}
